//这里重新根据使用的函数创建一个文件，扫描整个文件的范围，看看到底需要创建多大的文件
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>


#define MAX_LINE_SIZE 1024

void get_the_range_of_trance(char* trace_file, long* begin, long* end, long* buf_size,long* record_count){
    //这里获得一个trace文件的范围
    //首先获取trace文件
    //将起始offset和最大offset都创建出来，注意考虑size造成的偏移
    FILE *fp_read = NULL;
    char line[MAX_LINE_SIZE];

    //初始化范围
    *begin = -1;
    *end = -1;
    *buf_size = -1;
    *record_count = 0;
    

    //循环变量
    long j;
    int i;

    //临时记录当前访问范围的变量
    long begin_temp;
    long end_temp;
    long buf_size_temp;

    printf("开始运行\n");

    if((fp_read = fopen(trace_file, "r")) != NULL){
        printf("开始读取\n");

        char delims[] = ",";
        char *result = NULL;

        while (fgets(line, MAX_LINE_SIZE, fp_read)){
            //这里是个分割的元素
            result = strtok( line, delims );

            // printf("%s", result);

            //这里要进行计数，只保留第5列
            i = 1;


            //这里获取的是一个offset
            if(i == 1){
                begin_temp = atol(result);
                // printf("%ld\n", begin_temp);
                if(*begin == -1){
                    *begin = begin_temp;
                }else if(*begin > begin_temp){
                    *begin = begin_temp;
                }
            }


            //只要还有可以分的，那result不会是NULL
            while((result = strtok( NULL, delims))){
                i++;
                
                // if(i == 5){
                //     begin_temp = atol(result);
                //     // printf("%ld\n", begin_temp);
                //     if(*begin == -1){
                //         *begin = begin_temp;
                //     }else if(*begin > begin_temp){
                //         *begin = begin_temp;
                //     }
                // }
                //这里将offset也考虑在内
                if(i == 2){
                    buf_size_temp = atol(result);
                    end_temp = buf_size_temp + begin_temp;
                    // printf("%ld\n", end_temp);
                    if(*end == -1){
                        *end = end_temp;
                    }else if(*end < end_temp){
                        *end = end_temp;
                    }

                    if(*buf_size == -1){
                        *buf_size = buf_size_temp;
                    }else if(*buf_size < buf_size_temp){
                        *buf_size = buf_size_temp;
                    }
                }
            }
            
            (*record_count)++;
        }

        printf("读取完毕\n");
        fclose(fp_read);
        fp_read = NULL;
    }
}


//将访问数据读到内存
void get_access_information(char* trace_file, long* offset_arr, long* size_arr ,long record_count){
    //这里获得一个trace文件的范围
    //首先获取trace文件
    //将起始offset和最大offset都创建出来，注意考虑size造成的偏移
    FILE *fp_read = NULL;
    char line[MAX_LINE_SIZE];

    //循环变量
    long j;
    int i;

    //一个用来记录当前存储位置的变量
    long now_index = 0;

    //临时记录当前访问范围的变量
    long begin_temp;
    long size_temp;

    printf("开始运行\n");

    if((fp_read = fopen(trace_file, "r")) != NULL){
        printf("开始读取\n");

        char delims[] = ",";
        char *result = NULL;

        while (fgets(line, MAX_LINE_SIZE, fp_read)){
            //这里是个分割的元素
            result = strtok( line, delims );

            // printf("%s", result);

            //这里要进行计数，只保留第5列
            i = 1;

            if(now_index > record_count){
                printf("数组爆了\n");
                exit(-1);
            }

            if(i == 1){
                begin_temp = atol(result);
                // printf("%ld\n", begin_temp);
                
                offset_arr[now_index] = begin_temp;
            }

            //只要还有可以分的，那result不会是NULL
            while((result = strtok( NULL, delims))){
                i++;
                // if(i == 4){
                //     if(result[0] == 'R'){
                //         op_kind[now_index] = 0;
                //     }else{
                //         op_kind[now_index] = 1;
                //     }
                // }

                // if(i == 5){
                    
                // }
                
                //这里将offset也考虑在内
                if(i == 2){
                    size_temp = atol(result);
                    // printf("%ld\n", size_temp);
                    size_arr[now_index] = size_temp;
                }
            }

            now_index++;
        }

        printf("读取完毕\n");
        fclose(fp_read);
        fp_read = NULL;
    }
}

int main(){
    long begin;
    long end;
    long size;
    long record_count;

    //这里看看最大buf size
    long buf_size;

    long* offset_arr;
    long* size_arr;
    short* op_kind;
    FILE *fp_write = NULL;

    //用来写一个文件的空缓冲区
    char write_buf[1048576];
    printf("sizeof(char) = %lu\n",sizeof(char));

    int test_file;

    get_the_range_of_trance("./fix-tracecsv", &begin, &end, &buf_size, &record_count);

    printf("begin = %ld, end = %ld\n", begin, end);

    size = end - begin;

    if((fp_write = fopen("test", "w+")) == NULL){
        printf("文件写开启失败\n");
        return -1;
    }

    fclose(fp_write);
    
    int fd1;

    fd1=open("test",O_CREAT | O_RDWR,00777);
    if(fd1!=-1){
        printf("file is created.\n");
    }
    
    int j;
    printf("end = %ld\n", end);

    //我们注意只写16G的文件
    for(j = 0; j < 17179869184/1048576; j++){
        printf("写文件，第%d次\n",j);
        write(fd1,write_buf,1048576);
    }

    write(fd1,write_buf,1048576);

    close(fd1);
    // close(test_file);
    return 0;
}


